package com.example.composedialogdemo.dialog

import android.app.DatePickerDialog
import android.icu.util.Calendar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import java.time.LocalDate

/**
 * @author: njb
 * @date:   2025/8/17 0:23
 * @desc:   描述
 */
@Composable
fun DatePickerDialog(
    showDialog: Boolean,
    onDismiss: () -> Unit,
    onDateSelected: (LocalDate) -> Unit
) {
    val context = LocalContext.current
    val calendar = Calendar.getInstance()
    val year = calendar.get(Calendar.YEAR)
    val month = calendar.get(Calendar.MONTH)
    val day = calendar.get(Calendar.DAY_OF_MONTH)
    val dataPicker = remember {
        DatePickerDialog(
            context,
            { _, selectYear, selectMonth, selectDay ->
                onDateSelected(LocalDate.of(selectYear, selectMonth + 1, selectDay))
            },
            year,
            month,
            day
        )
    }
    if (showDialog) {
        DisposableEffect(Unit) {
            dataPicker.show()
            onDispose {
                dataPicker.dismiss()
            }
        }
    }
}